package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;

import com.hyphenate.easeui.utils.Preferences;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.orhanobut.logger.Logger;

/**
 * @author NineTailDemonFox
 * @date 2019/7/25 13:57
 * 功能描述： 启动界面
 * 联系方式：1037438704@qq.com
 */
public class SplashActivity extends AppCompatActivity {

    private LinearLayout mLlAd;
    private int count;
    private String nickname, invite_code, token, sex, account, getvip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mLlAd = findViewById(R.id.ll_ad);
        count = Preferences.getInstance().getInt(this, "page", "count");
        Preferences.getInstance().commit(SplashActivity.this, "page", "count", count + 1);
        token = Preferences.getInstance().getString(this, "user", "token");
        account = Preferences.getInstance().getString(this, "name", "phone");
        nickname = Preferences.getInstance().getString(SplashActivity.this, "user", "nickname");
        invite_code = Preferences.getInstance().getString(SplashActivity.this, "user", "invite_code");
        getvip = Preferences.getInstance().getString(SplashActivity.this, "user", "vip");
        sex = Preferences.getInstance().getString(SplashActivity.this, "user", "sex");
        jumpToMain();
    }


    private void jumpToMain() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1f);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setFillAfter(true);
        mLlAd.startAnimation(alphaAnimation);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Logger.d("=========登录界面获取的性别====nickname======" + nickname);
                Logger.d("=========登录界面获取的性别===invite_code=======" + invite_code);
                if (count == 0) {
                    startActivity(new Intent(SplashActivity.this, GuidePageActivity.class));
                } else {
                    if (TextUtils.isEmpty(token)) {
                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                    } else {
                        // 性别 1男 2 女
                        Logger.d("=========登录界面获取的性别==========" + sex);
                        if (TextUtils.isEmpty(sex) || sex.equals("0")) {
                            startActivity(new Intent(SplashActivity.this, SelectiveSexActivity.class));
                        } else if (sex.equals("1")) {
                            if (!TextUtils.isEmpty(invite_code)) {
                                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                            } else {
                                if (getvip.equals("2")) {
                                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                                } else {
                                    startActivity(new Intent(SplashActivity.this, WelcomeActivity.class));
                                }
                            }
                        } else if (sex.equals("2")) {
                            if (!TextUtils.isEmpty(nickname)) {
                                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                            } else {
                                EditorialMaterialsActivity.start(SplashActivity.this, 1);
                            }
                        }
                    }
                }
                finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 防止用户返回键退出APP
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return keyCode == KeyEvent.KEYCODE_BACK
                || keyCode == KeyEvent.KEYCODE_HOME
                || super.onKeyDown(keyCode, event);
    }
}
